package com.sq.partner.manager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sq.partner.manager.util.JMXUtils;
import com.sq.partner.manager.util.LogUtil;


/**
 * log4j管理
 * 
 * @author 
 * 
 */
public class LogManager {
	private static Logger logger = LogUtil.getLog(LogManager.class);

	/** 服务名称，如"福建交易流量" */
	private String name;
	/** JMX的serviceURL */
	private Map<String,String> serviceURL;
	/** JMX的objectName */
	private String objectName;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Map<String, String> getServiceURL() {
		return serviceURL;
	}

	public void setServiceURL(Map<String, String> serviceURL) {
		this.serviceURL = serviceURL;
	}

	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}


	/**
	 * 获取指定目标的日志级别
	 * @param target
	 * @return
	 */
	public List<String> getTargetLevel(String target,String ip)throws  Exception {
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"getTargetLevel", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;
	}
	/**
	 * 设置目标的日志级别为INFO
	 * @param target
	 * @return
	 */
	public List<String> assignInfoLevel(String target,String ip)throws  Exception  {
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"assignInfoLevel", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;
	}
	
	/**
	 * 设置目标的日志级别为DEBUG
	 * @param target
	 * @return
	 */
	public List<String> assignDebugLevel(String target,String ip)throws  Exception{
			List<String> resutl = new ArrayList<String>();
			for(Map.Entry<String, String> map:serviceURL.entrySet()){
				if(null != ip && !"".equals(ip)){
					if(!ip.equals(map.getKey())){
						continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
					}
			}
				String str=(String) JMXUtils.invoke(map.getValue(), objectName,"assignDebugLevel", new Object[]{target}, new String[] { "java.lang.String" });
				resutl.add(str);
			}
			return resutl;
	}

	/**
	 * 设置目标的日志级别为WARN
	 * @param target
	 * @return
	 */
	public List<String> assignWarnLevel(String target,String ip)throws  Exception {
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"assignWarnLevel", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;	
	}

	/**
	 * 设置目标的日志级别为ERROR
	 * @param target
	 * @return
	 */
	public List<String> assignErrorLevel(String target,String ip) throws  Exception{
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"assignErrorLevel", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;		
	}
	
	

	/**
	 * 设置目标的日志级别为TRACE
	 * @param target
	 * @return
	 */
	public List<String> assignTraceLevel(String target,String ip)throws  Exception{
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"assignTraceLevel", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;		
	}
	
	/**
	 * 关闭目标的日志
	 * @param target
	 * @return
	 */
	public List<String> deactivateLogging(String target,String ip)throws  Exception {
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"deactivateLogging", new Object[]{target}, new String[] { "java.lang.String" });
			resutl.add(str);
		}
		return resutl;	
		
	}
	
	/**
	 * 打印log4j.properties的内容
	 */
	public List<String> printLog4jConfig(String ip) throws  Exception{
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
		}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"printLog4jConfig",  new Object[0], new String[0]);
			resutl.add(str);
		}
		return resutl;
	}
	
	/**
	 * 根据mode提取loggers
	 * @param mode:ALL(所有logger，缺省)、LEVELED(在log4j.propertied中配置的logger)
	 * @return
	 */
	public List<Map<String,String>> getLoggers(String target,String ip)throws  Exception {
		List<Map<String,String>> resutl = new ArrayList<Map<String,String>>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
			}
			Map<String,String> str=null;
			try{
				 str=(Map<String,String>) JMXUtils.invoke(map.getValue(), objectName,"getLoggers", new Object[]{target}, new String[] { "java.lang.String" });
				
			}catch(Exception e){
				logger.error(e);
			}
			resutl.add(str);
		}
		return resutl;
	}
	
	/**
	 * 重新加载log4j.properties
	 * @return
	 */
	public List<String> resetConfiguration(String ip)throws  Exception {
		List<String> resutl = new ArrayList<String>();
		for(Map.Entry<String, String> map:serviceURL.entrySet()){
			if(null != ip && !"".equals(ip)){
				if(!ip.equals(map.getKey())){
					continue; //如果传入服务器ip 和配置中比较 过滤掉不匹配的
				}
			}
			String str=(String) JMXUtils.invoke(map.getValue(), objectName,"resetConfiguration", new Object[0], new String[0]);
			resutl.add(str);
		}
		return resutl;
	}
}
